package org.ycx.upms.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ycx.common.annotation.BaseService;
import org.ycx.common.base.BaseServiceImpl;
import org.ycx.common.db.DataSourceEnum;
import org.ycx.common.db.DynamicDataSource;
import org.ycx.upms.dao.mapper.ISecAttachmentMapper;
import org.ycx.upms.dao.mapper.ISecAttachmentUserMapper;
import org.ycx.upms.dao.mapper.ISecUserMapper;
import org.ycx.upms.dao.model.*;
import org.ycx.upms.service.ISecAttachmentService;

import java.util.List;

/**
* SecAttachmentService实现
* Created by yang on 2017-07-19 15:16:03.
*/
@Service
@Transactional
@BaseService
public class SecAttachmentServiceImpl extends BaseServiceImpl<ISecAttachmentMapper, SecAttachment, SecAttachmentExample> implements ISecAttachmentService {

    private static Logger _log = LoggerFactory.getLogger(SecAttachmentServiceImpl.class);

    @Autowired
    ISecAttachmentMapper mapper;

    @Autowired
    ISecUserMapper userMapper;

    @Autowired
    ISecAttachmentUserMapper attachmentUserMapper;

    @Override
    public List<SecUser> selectUserByFile(Integer fid) {

        return this.userMapper.selectUserByAttach(fid);

    }

    @Override
    public int addFileUsers(Integer fid, String loginName) {
        DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        SecUserExample sue = new SecUserExample();
        sue.createCriteria().andLoginNameEqualTo(loginName);
        List<SecUser> user = this.userMapper.selectByExample(sue);
        SecAttachmentUser attUser = new SecAttachmentUser();
        attUser.setAttId(fid);
        attUser.setUserId(user.get(0).getUid());
        return this.attachmentUserMapper.insert(attUser);
    }
}